const app = getApp()
Component({
    properties: {},
    data: {
        user: {
            username: '',
            uicon: 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0',
            loginStatus: true,
            uDeviceToken: '',
        }
    }, lifetimes: {
        attached: function () {
            console.log("attached")
            console.log(app.globalData.loginStatus)
            if (app.globalData.loginStatus == false) {
                this.setData({
                    'user.uicon': app.globalData.iconUrl,
                    'user.username': app.globalData.username,
                    'user.loginStatus': app.globalData.loginStatus,
                    'user.uDeviceToken': app.globalData.token
                });
            }
        }


    },
    methods: {
        login: function () {
            console.log("login")
            wx.login({ //发起请求判断是否已经注册
                success: (res) => { //请求成功赋值给变量
                    if (res.code) { //变量获取code
                        console.log(res.code)
                        wx.request({
                            url: 'http://43.143.141.18:8081/login/login?code=' + res.code,  //这里是请求地址.
                            method: 'GET',  //请求方式
                            success: (res) => {
                                console.log(res)
                                if (res.data.false != null) {
                                    app.globalData.uuid = res.data.false.id
                                    wx.navigateTo({
                                        url: '/pages/register/index'
                                    });
                                } else {
                                    app.globalData.iconUrl = res.data.true.url
                                    app.globalData.token = res.data.true.grant
                                    app.globalData.loginStatus = false
                                    app.globalData.username = res.data.true.name
                                    this.setData({
                                        'user.uicon': res.data.true.url,
                                        'user.username': res.data.true.name,
                                        'user.loginStatus': false,
                                        'user.uDeviceToken': res.data.true.grant
                                    });
                                    console.log("this.user.iconUrl")
                                    console.log(res.data.true.url)
                                    wx.showToast({
                                        title: '登录成功',
                                        icon: 'success',
                                        duration: 2000
                                    });
                                }

                            }
                        })
                    }
                },
            })

        },
        setLoginStatus: function (status) {
            this.setData({
                'user.loginStatus': status
            });
        },
        logout: function () {
            this.setData({
                'user.loginStatus': true,
                'user.uicon': 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0',
                'user.username': '',
                'user.uDeviceToken': ''

            });
            app.globalData.loginStatus = true
            app.globalData.iconUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'
            app.globalData.token = ''
            app.globalData.username = ''

            wx.showToast({
                title: '退出成功',
                icon: 'success',
                duration: 2000
            });
        }

    },
});
